<?php
namespace app\admin\model;

use app\admin\model\Channel;
use think\Model;
use think\Db;

/**
 * 签名管理模型
 * @package app\admin\model
 */
class Signm extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'bus_sign';

    /**
     * [getSignAll1 针对新版api处理]
     * @author   .guo
     * @DataTime 2019-05-15
     * @param    [type]     $request [description]
     * @return   [type]              [description]
     */
    public function getSignAll($request)
    {
        $user_type = session('user_type');
        $relate_id = session('relate_id');

        $data = $this
            ->field('bus_sign.*,adm_user.username')
            ->join('adm_user','adm_user.id=bus_sign.user_id','left')
            ->join('bus_channel','bus_channel.channel_id=bus_sign.channel_id','left');

        switch ($user_type) {
            case 'admin':
                break;
            case 'channel':
                if(is_OneCha())
                {
                    $data = $data->where(function($query){
                        $query->where('bus_channel.p_id',session('relate_id'))->whereor('bus_sign.channel_id',session('relate_id'));
                    });
                }else{
                    $data = $data->where('bus_sign.channel_id',$relate_id);
                }
                break;

            default:
                /*写死通道角色权限*/
                if(session('username') == 'admin2018'){
                }else{
                    return;
                }
                break;
        }

        if(!empty($request['sign'])) {
            $data = $data->where('bus_sign.sign', 'like' , '%' . $request['sign'] . '%' );
        }

        if(!empty($request['status'])) {

            $data = $data->where('bus_sign.status', $request['status']);
        }

        if(!empty($request['ci'])) {

            $data = $data->where('bus_sign.channel_id', $request['ci']);
        }

        $data = $data->order('bus_sign.status,bus_sign.create_time desc')
            ->paginate($request['limit'], false,['query' => $request])
            ->each(function($item, $key){
                switch ($item->status) {
                    case '1':
                        $item->status_ = '<span class="layui-badge layui-bg-green">已通过</span>';
                        break;
                    case '2':
                        $item->status_ = '<span class="layui-badge layui-bg-orange">待审核</span>';
                        break;
                    case '3':
                        $item->status_ = '<span class="layui-badge">未通过</span>';
                        break;

                    default:
                        # code...
                        break;
                }
            });
        return $data;
    }

    /**
     * 添加签名模板
     * @Author   lf
     * @DateTime 2018-07-03T09:53:25+0800
     * @param    [type]                   $relate_id [绑定的渠道id]
     * @param    [type]                   $sign      [签名内容]
     */
    public function addSignOne($relate_id,$sign)
    {
    	try {
			//实例化模型添加数据
			$signm = new Signm;
			$signm->user_id     = session('user_id');
			$signm->channel_id  = $relate_id;
			$signm->sign        = $sign;
			$signm->status      = '1';
			$signm->create_time = date('Y-m-d H:i:s');
			$signm->isUpdate(false)->save();
			trace('用户'.session('username')."申请签名模板：\r\n".json_encode($signm,JSON_UNESCAPED_UNICODE),'info');
            return 'success';

    	} catch (Exception $e) {
    		trace('用户'.session('username')."申请签名模板失败\r\n".$e,'info');
            return '申请失败，服务器繁忙';
    	}
    }

    /**
     * 签名审核
     * @Author   lf
     * @DateTime 2018-07-03T10:00:50+0800
     * @param    [type]                   $signid   [签名id]
     * @param    [type]                   $status   [审核状态]
     * @param    [type]                   $describe [描述]
     */
    public function setSignApply($signid,$status,$describe)
    {
    	try {
    		//实例化模型添加数据
			$signm = Signm::get($signid);
			$signm->status      = $status;
			$signm->describe    = $describe;
			$signm->apply_user  = session('user_id');
			$signm->apply_time  = date('Y-m-d H:i:s');
			$signm->isUpdate(true)->save();
			trace('用户'.session('username')."审核签名模板：\r\n".json_encode($signm,JSON_UNESCAPED_UNICODE),'info');
			echo "success";

    	} catch (Exception $e) {
    		trace('用户'.session('username')."审核签名失败\r\n".$e,'info');
    		echo "审核失败，服务器繁忙";
    	}
    }

}